/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.jome.ctrl.mml;

/**
 *
 * @author Mr.Sam
 */
import java.awt.*;
import java.awt.event.*;
import java.io.*;

import fr.ove.openmath.jome.ctrl.linear.events.*;
import fr.ove.openmath.jome.ctrl.mml.*;
    
public class TestMMLParser extends Frame {
    MathMLParser parser = new MathMLParser();
    
    TextArea mml = new TextArea() {
        public Dimension getPreferredSize() {
            Dimension dim = super.getPreferredSize();
            dim.width /= 1.5;
            
            return dim;
        }
    };
    
    EventViewer events = new EventViewer();
    Button parse = new Button("Parse");
    
    
    public TestMMLParser() {
        parser.addLinearParserListener(events);
        setBounds(50, 50, 640, 480);
        setLayout(new BorderLayout());
        
        addWindowListener(
            new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    Frame src = (Frame) evt.getSource();
            		src.setVisible(false);	// hide the Frame
            		src.dispose();			// free the system resources
            		System.exit(0);		// close the application
                }
            }
        );
        
        add(mml, "West");
        add(events, "Center");
        add(parse, "South");
        
        parse.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    events.setText("");
                    String mmlObject = mml.getText();
                    ByteArrayInputStream in = new ByteArrayInputStream(mmlObject.getBytes());
                    try {
                        parser.parse(in);
                    }
                    catch(IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
            }
        );
    }        
        
    public static void main(String args[]) {
        (new TestMMLParser()).setVisible(true);
    }
}


class EventViewer extends TextArea implements LinearParserListener {
    /**
    * Treats the event received.
    * @param linearParserEvent the event received.
    */
    public void consumeLinearParserEvent(LinearParserEvent linearParserEvent) {
        append(linearParserEvent.toString() + "\n");
    }
    
}